package com.fayuan.nocv.TXApi;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpUtils2 {

    //请求参数
    public String getData() throws IOException {
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(10000)
                .setConnectTimeout(10000)
                .setConnectionRequestTimeout(10000)
                .build();

        CloseableHttpClient httpClient = null;
        HttpGet request = null;
        CloseableHttpResponse response = null;

        try {
            //创建Httpclient
            httpClient = HttpClients.createDefault();
            //发送网络请求
            request = new HttpGet("https://api.iwyu.com/API/weibo");
            //配置信息
            request.setConfig(requestConfig);
            //发送请求
            response = httpClient.execute(request);
            //查看状态码
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode==200){
                HttpEntity entity = response.getEntity();
                String string = EntityUtils.toString(entity,"utf-8");
                return string;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (response!=null){
                response.close();
            }
            if (request!=null){
                request.releaseConnection();
            }
            if (httpClient!=null){
                httpClient.close();
            }
        }
        return null;
    }
}
